package com.kong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kong.constant.MessageConstant;
import com.kong.entity.Result;
import com.kong.pojo.OrderSetting;
import com.kong.service.OrderSettingService;
import com.kong.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @author yang2013
 * @data 2020/10/12 17:43
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;
    /**
     * 从excel中读取数据---并将数据插入到数据库
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile multipartFile){
        try {
            List<String[]> excelList = POIUtils.readExcel(multipartFile);

            List<Map> mapList=new ArrayList<>();
            if (excelList!=null && excelList.size()>0){
                for (String[] strings : excelList) {
                    Map map=new HashMap();
                    map.put("orderDate",new Date(strings[0]));
                    map.put("reservations",Integer.parseInt(strings[1]));
                    mapList.add(map);
                }
                orderSettingService.batchInsert(mapList);
            }
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 修改单个预约表的数据
     */
    @PostMapping("/update")
    public Result updateNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.updateOrderSetting(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 日历组件数据填充
     */
    @GetMapping("/findAll")
    public Result findAll(String orderDate){
        try {
            List<Map> mapList = orderSettingService.findAll(orderDate);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,mapList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }


}
